{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import youtokentome as yttm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "files = ['../pure-text/filtered-dumping-wiki.txt',\n",
    "        '../pure-text/dumping-cleaned-news.txt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "for file in files:\n",
    "    with open(file) as fopen:\n",
    "        results.extend(list(filter(None, fopen.read().split('\\n'))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5521509"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('concat.txt', 'w') as fopen:\n",
    "    fopen.write('\\n'.join(results))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "bpe = yttm.BPE.train(data='concat.txt', vocab_size=32000, model='segmentation.yttm',\n",
    "                    pad_id = 0, unk_id = 3, bos_id = 2, eos_id = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = 'SekolahMenengah KebangsaanCahaya Saktiatau namaringkasnyaCahayaSakti\",merupakansebuahSekolahMenengahKebangsaanSwastayangterletak di.OttoIskandardinataI/11,JakartaTimur,Indonesia.SekolahMenengah Kebangsaan CahayaSaktimemilikikod20103715.Pada2010,CahayaSaktimemiliki27pelajar lelaki dan66 pelajar perempuan,menjadikanjumlahkeseluruhan muridseramai93 orang.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 911 µs, sys: 211 µs, total: 1.12 ms\n",
      "Wall time: 1.13 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['SekolahMenengah KebangsaanCahaya Saktiatau namaringkasnyaCahayaSakti\",merupakansebuahSekolahMenengahKebangsaanSwastayangterletak di.OttoIskandardinataI/11,JakartaTimur,Indonesia.SekolahMenengah Kebangsaan CahayaSaktimemilikikod20103715.Pada2010,CahayaSaktimemiliki27pelajar lelaki dan66 pelajar perempuan,menjadikanjumlahkeseluruhan muridseramai93 orang.']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "bpe.decode(bpe.encode(string, output_type=yttm.OutputType.ID))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1377,\n",
       " 29,\n",
       " 622,\n",
       " 1184,\n",
       " 1612,\n",
       " 49,\n",
       " 3634,\n",
       " 21658,\n",
       " 7061,\n",
       " 722,\n",
       " 7525,\n",
       " 1344,\n",
       " 3121,\n",
       " 49,\n",
       " 3634,\n",
       " 27,\n",
       " 14406,\n",
       " 1542,\n",
       " 8538,\n",
       " 840,\n",
       " 6451,\n",
       " 27,\n",
       " 14599,\n",
       " 29,\n",
       " 622,\n",
       " 1184,\n",
       " 32,\n",
       " 714,\n",
       " 1327,\n",
       " 27,\n",
       " 2826,\n",
       " 651,\n",
       " 857,\n",
       " 919,\n",
       " 638,\n",
       " 24,\n",
       " 61,\n",
       " 11,\n",
       " 3478,\n",
       " 39,\n",
       " 15,\n",
       " 3282,\n",
       " 624,\n",
       " 1425,\n",
       " 873,\n",
       " 39,\n",
       " 3284,\n",
       " 2332,\n",
       " 47,\n",
       " 2272,\n",
       " 38,\n",
       " 1529,\n",
       " 23,\n",
       " 39,\n",
       " 11102,\n",
       " 24,\n",
       " 27,\n",
       " 14599,\n",
       " 29,\n",
       " 622,\n",
       " 1184,\n",
       " 1612,\n",
       " 20219,\n",
       " 27,\n",
       " 633,\n",
       " 2242,\n",
       " 637,\n",
       " 1500,\n",
       " 715,\n",
       " 941,\n",
       " 1974,\n",
       " 34,\n",
       " 7855,\n",
       " 36,\n",
       " 2074,\n",
       " 26,\n",
       " 663,\n",
       " 1974,\n",
       " 2973,\n",
       " 49,\n",
       " 3634,\n",
       " 27,\n",
       " 633,\n",
       " 2242,\n",
       " 637,\n",
       " 4927,\n",
       " 8543,\n",
       " 18,\n",
       " 2827,\n",
       " 1624,\n",
       " 654,\n",
       " 11205,\n",
       " 1487,\n",
       " 4100,\n",
       " 1959,\n",
       " 25,\n",
       " 3564,\n",
       " 25,\n",
       " 1116,\n",
       " 9086,\n",
       " 2098,\n",
       " 2715,\n",
       " 16230,\n",
       " 1138,\n",
       " 21165,\n",
       " 2837]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bpe.encode(string, output_type=yttm.OutputType.ID)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm concat.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import youtokentome as yttm\n",
    "\n",
    "bpe = yttm.BPE(model='segmentation.yttm')\n",
    "\n",
    "class Encoder:\n",
    "    def __init__(self, bpe):\n",
    "        self.bpe = bpe\n",
    "        self.vocab_size = len(self.bpe.vocab())\n",
    "    \n",
    "    def encode(self, s):\n",
    "        s = self.bpe.encode(s, output_type=yttm.OutputType.ID)\n",
    "        s = [i + [1] for i in s]\n",
    "        return s\n",
    "    \n",
    "    def decode(self, ids, strip_extraneous=False):\n",
    "        return self.bpe.decode(list(ids))[0]\n",
    "    \n",
    "encoder = Encoder(bpe)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
